/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DumpSessions
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  41 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  43 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*  44 */     checkRight(zsc, context, localServer, Rights.Admin.R_getSessions);
/*     */     
/*  46 */     Element response = zsc.createElement(AdminConstants.DUMP_SESSIONS_RESPONSE);
/*     */     
/*  48 */     boolean includeAccounts = request.getAttributeBool("listSessions", false);
/*  49 */     boolean groupByAccount = request.getAttributeBool("groupByAccount", false);
/*  50 */     int totalActiveSessions = 0;
/*  51 */     Provisioning prov = Provisioning.getInstance();
/*     */     Element e;
/*  53 */     for (Session.Type type : Session.Type.values())
/*  54 */       if (type != Session.Type.NULL)
/*     */       {
/*     */ 
/*  57 */         if (!includeAccounts) {
/*  58 */           int[] stats = SessionCache.countActive(type);
/*     */           
/*  60 */           if (stats[1] != 0)
/*     */           {
/*     */ 
/*  63 */             Element e = response.addElement(type.name().toLowerCase());
/*  64 */             totalActiveSessions += stats[1];
/*  65 */             e.addAttribute("activeAccounts", stats[0]);
/*  66 */             e.addAttribute("activeSessions", stats[1]);
/*     */           }
/*  68 */         } else { List<Session> sessions = SessionCache.getActiveSessions(type);
/*  69 */           if (sessions.size() != 0)
/*     */           {
/*     */ 
/*  72 */             e = response.addElement(type.name().toLowerCase());
/*  73 */             totalActiveSessions += sessions.size();
/*  74 */             e.addAttribute("activeSessions", sessions.size());
/*  75 */             if (sessions.size() != 0)
/*     */             {
/*     */               Element acctElt;
/*  78 */               if (groupByAccount)
/*     */               {
/*  80 */                 HashMap<String, List<Session>> map = new HashMap();
/*  81 */                 for (Session s : sessions) {
/*  82 */                   List<Session> list = (List)map.get(s.getAuthenticatedAccountId());
/*  83 */                   if (list == null) {
/*  84 */                     list = new ArrayList();
/*  85 */                     map.put(s.getAuthenticatedAccountId(), list);
/*     */                   }
/*  87 */                   list.add(s);
/*     */                 }
/*     */                 
/*  90 */                 e.addAttribute("activeAccounts", map.size());
/*     */                 
/*  92 */                 for (Map.Entry<String, List<Session>> entry : map.entrySet()) {
/*  93 */                   acctElt = e.addElement("zid");
/*  94 */                   acctElt.addAttribute("name", getName(prov, (String)entry.getKey()));
/*  95 */                   acctElt.addAttribute("id", (String)entry.getKey());
/*  96 */                   for (Session s : (List)entry.getValue()) {
/*  97 */                     encodeSession(acctElt, s, false, prov);
/*     */                   }
/*     */                 }
/*     */               } else {
/* 101 */                 int[] stats = SessionCache.countActive(type);
/* 102 */                 e.addAttribute("activeAccounts", stats[0]);
/*     */                 
/* 104 */                 for (Session s : sessions)
/* 105 */                   encodeSession(e, s, true, prov);
/*     */               }
/*     */             }
/*     */           }
/*     */         } }
/* 110 */     response.addAttribute("activeSessions", totalActiveSessions);
/*     */     
/* 112 */     return response;
/*     */   }
/*     */   
/*     */   private static String getName(Provisioning prov, String id) {
/*     */     try {
/* 117 */       Account acct = prov.get(Key.AccountBy.id, id);
/* 118 */       return acct == null ? id : acct.getName();
/*     */     } catch (ServiceException e) {}
/* 120 */     return id;
/*     */   }
/*     */   
/*     */   private static void encodeSession(Element parent, Session s, boolean includeAcct, Provisioning prov)
/*     */   {
/* 125 */     Element sElt = parent.addElement("s");
/* 126 */     if (includeAcct) {
/* 127 */       sElt.addAttribute("zid", s.getAuthenticatedAccountId());
/* 128 */       sElt.addAttribute("name", getName(prov, s.getAuthenticatedAccountId()));
/*     */     }
/* 130 */     sElt.addAttribute("sid", s.getSessionId());
/* 131 */     sElt.addAttribute("cd", s.getCreationTime());
/* 132 */     sElt.addAttribute("ld", s.getLastAccessTime());
/* 133 */     s.encodeState(sElt);
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 138 */     relatedRights.add(Rights.Admin.R_getSessions);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/DumpSessions.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */